package com.ashley.apptools.category;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.ashley.apptools.R;
import com.ashley.apptools.interfaces.IActivity;
import com.ashley.apptools.provider.ToolsData.Category;
import com.ashley.apptools.provider.ToolsData.ItemNumber;
import com.ashley.apptools.utils.CategoryContentResolver;
import com.ashley.apptools.utils.ItemNoContentResolver;
import com.ashley.apptools.vo.CategoryVo;
import com.ashley.apptools.vo.IValue;
import com.ashley.apptools.vo.ItemVo;

public class CategoryDefaultvalue implements IActivity {

	private Activity mContext;
	private Spinner categorySpinner;
	private Spinner categoryTitleSpinner;
	private Spinner itemNoSpinner;
	private Thread thread;
	
	private EditText taxEditText;
	private EditText landingEditText;
	private EditText saleEditText;
	private EditText warehovseEditText;
	public CategoryDefaultvalue(Activity activity) {
		// TODO Auto-generated constructor stub
		this.mContext = activity;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		categorySpinner = (Spinner) mContext.findViewById(R.id.category_sp);
		categoryTitleSpinner = (Spinner) mContext.findViewById(R.id.category_title);
		itemNoSpinner = (Spinner) mContext.findViewById(R.id.item_ititle);
		
		taxEditText = (EditText) mContext.findViewById(R.id.tax_edit);
		landingEditText = (EditText) mContext.findViewById(R.id.landing_edit);
		warehovseEditText = (EditText) mContext.findViewById(R.id.warehovse_edit);
		saleEditText = (EditText) mContext.findViewById(R.id.sale_edit);
		
		getCategory();
		
	}
	
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onRestart() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub

	}
	
	private void getCategory()
	{
		final ContentResolver contentResolver = mContext.getContentResolver();
		
		if(thread !=null&&thread.isAlive())
		{
			thread.interrupt();
		}
		thread = new Thread()
		{
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Cursor cursor = CategoryContentResolver.getCategory(contentResolver,null, CategoryContentResolver.DEFAULT_ID_SORT_ORDER);
				handler.obtainMessage(0, cursor).sendToTarget();
				super.run();
			}
		};
		thread.start();
	}
	
	private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {

		private Cursor cursor;
		@Override
		
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub

				if(arg0 == categoryTitleSpinner)
				{
					cursor = (Cursor) arg0.getSelectedItem();
					cursor.moveToFirst();
					if(cursor.moveToPosition((int) arg2))
					{
						CategoryVo categoryVo = CategoryContentResolver.cursorToCategoryVo(cursor, 1);
						onChange(categoryVo);
					}
					if(itemNoSpinner.getAdapter() !=null)
					{
						SimpleCursorAdapter adapter = (SimpleCursorAdapter) itemNoSpinner.getAdapter();
						adapter.getCursor().close();
					}
					getItemNo((int) arg3);
				}else if(arg0 == categorySpinner)
				{
					cursor = (Cursor) arg0.getSelectedItem();
					cursor.moveToFirst();
					if(landingEditText.length() == 0 && warehovseEditText.length() == 0 &&  saleEditText.length() == 0 )
					{
						CategoryVo categoryVo = CategoryContentResolver.cursorToCategoryVo(cursor, 1);
						onChange(categoryVo);
					}
					
				}else if(arg0 == itemNoSpinner)
				{
					cursor = (Cursor) arg0.getSelectedItem();
					cursor.moveToFirst();
					if(cursor.moveToPosition((int) arg2))
					{
						ItemVo itemVo = ItemNoContentResolver.cursorToItemVo(cursor, 1);
						onChange(itemVo);
					}
				}
			
			
		}
		
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	};
	private void onChange(IValue value)
	{
		handler.obtainMessage(2, value).sendToTarget();
	}
	private void getItemNo(int id)
	{
		final ContentResolver contentResolver = mContext.getContentResolver();
		final int uid = id;
		if(thread !=null&&thread.isAlive())
		{
			thread.interrupt();
		}
		thread = new Thread()
		{
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				Cursor cursor = ItemNoContentResolver.getCategoryItem(contentResolver, null, uid, ItemNoContentResolver.ITEM_SORT_ORDER);

				handler.obtainMessage(1, cursor).sendToTarget();

				super.run();
			}
		};
		
		thread.start();
		
	}
	
	private Handler handler = new Handler(){
		private SimpleCursorAdapter adapter;
		private Cursor cursor;
		
		@SuppressWarnings("deprecation")
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				cursor = (Cursor) msg.obj;
				adapter = new SimpleCursorAdapter(mContext,
						  android.R.layout.simple_spinner_item,
						  cursor, 
						  new String[]{Category.COLUMN_NAME_TITLE},
						  new int[]{android.R.id.text1})
				{
					public int getCount() {
						int i = super.getCount() -1;
						return i;
					};
				};
			
				adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				categorySpinner.setAdapter(adapter);
				
				categoryTitleSpinner.setAdapter(adapter);
				categorySpinner.setSelection(cursor.getCount()-1, true);
				categoryTitleSpinner.setSelection(cursor.getCount()-1, true);
				categorySpinner.setOnItemSelectedListener(itemSelectedListener);
				categoryTitleSpinner.setOnItemSelectedListener(itemSelectedListener);
				
				break;
			case 1:
				cursor = (Cursor) msg.obj;
				if(cursor.getCount()>0)
				{
					adapter = new SimpleCursorAdapter(mContext,
							  android.R.layout.simple_spinner_item,
							  cursor, 
							  new String[]{ItemNumber.COLUME_ITEM_NO},
							  new int[]{android.R.id.text1});
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					
					itemNoSpinner.setAdapter(adapter);
					itemNoSpinner.setOnItemSelectedListener(itemSelectedListener);
				}
				
				break;
			case 2:
				IValue value = (IValue) msg.obj;
				
				
				taxEditText.setText(String.valueOf(value.getTax()));
				landingEditText.setText(String.valueOf(value.getLanding()));
				warehovseEditText.setText(String.valueOf(value.getWarehouse()));
				saleEditText.setText(String.valueOf(value.getSale()));
				break;
			}
		};
	};
	
}
